package cc.site.main.service.impl;

import cc.site.main.bean.TblKind;
import cc.site.main.bean.TblPet;
import cc.site.main.bean.TblRoom;
import cc.site.main.bean.TblUser;
import cc.site.main.bean.dto.PetDto;
import cc.site.main.mapper.TblPetMapper;
import cc.site.main.service.TblKindService;
import cc.site.main.service.TblPetService;
import cc.site.main.service.TblRoomService;
import cc.site.main.service.TblUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 宠物信息 服务实现类
 * </p>
 *
 * @author yangruomao
 * @since 2024-04-14
 */
@Service
public class TblPetServiceImpl extends ServiceImpl<TblPetMapper, TblPet> implements TblPetService {


    @Autowired
    private TblRoomService roomService;

    @Autowired
    private TblUserService userService;

    @Autowired
    private TblKindService kindService;

    @Override
    public List<PetDto> all() {
        List<PetDto> res = new ArrayList<>();

        list().forEach(e -> {
            PetDto dto = new PetDto();
            BeanUtils.copyProperties(e, dto);

            Integer kindId = e.getKindId();
            Integer roomId = e.getRoomId();
            Integer fosterId = e.getFosterHumanId();
            Integer masterId = e.getMasterHumanId();

            TblKind kind = kindService.getById(kindId);
            dto.setKind(kind);
            TblRoom room = roomService.getById(roomId);
            dto.setRoom(room);
            TblUser foster = userService.getById(fosterId);
            dto.setFosterMan(foster);
            if (masterId != -1) {
                TblUser master = userService.getById(masterId);
                dto.setMasterMan(master);
            }


            res.add(dto);
        });

        return res;
    }
}
